package com.study.controller;

import com.study.dao.entity.MovieUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;


@RestController
public class MovieUserController {
	
	@Autowired
	LoadBalancerClient LoadBalancerClient;

	/*
	    异步的请求方式
		http://127.0.0.1:8082/webflux/1
	 */
	@GetMapping("/webflux/{id}")
	public MovieUser selectById(@PathVariable("id") int id) {

//		WebClient client = WebClient.create("http://sunxj-microservice-provider-user");
		WebClient client = WebClient.create("http://127.0.0.1:8080");
		Mono<MovieUser> mono = client.get()
				.uri("/simple/" + id, "1")
				.retrieve()
				.bodyToMono(MovieUser.class);
		System.out.println("time1" + System.currentTimeMillis());
		MovieUser movieUser = mono.block();   //在这里阻塞
		System.out.println("time2" + System.currentTimeMillis());
		return movieUser;
	}
	
	@GetMapping("/test")
	public String test() {
		ServiceInstance instance = LoadBalancerClient.choose("sunxj-microservice-provider-user");
		System.out.println(instance.getHost()+":"+instance.getPort());
		
		return "1";
	}
	

}
